import NProgress from "nprogress";
import "nprogress/nprogress.css";

// 微应用注册信息
import apps from "./app";

const state = {}
const actions = qiankun.initGlobalState(state);
actions.setGlobalState({
  globalToken: ''
})

// qiankun.registerMicroApps(apps, {
//   beforeLoad: (app) => {
//     // 加载微应用前，加载进度条
//     NProgress.start();
//     console.log("before load", app.name);
//     return Promise.resolve();
//   },

//   afterMount: (app) => {
//     // 加载微应用前，进度条加载完成
//     NProgress.done();
//     return Promise.resolve();
//   },
// });
qiankun.prefetchApps(apps)
const childApp = () => {
  return qiankun.loadMicroApp(apps[0], {
    sandbox: { strictStyleIsolation: false },
    // 否为单实例场景，单实例指的是同一时间只会渲染一个微应用
    singular: true
  })
}

qiankun.addGlobalUncaughtErrorHandler((event) => {
  console.error(event);
  const { message: msg } = event
  if (msg && msg.includes("died in status LOADING_SOURCE_CODE")) {
    console.error("微应用加载失败，请检查应用是否可运行");
  }
});


export default {
  actions,
  childApp,
  microsStart: qiankun.start
}